<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row  :gutter="20">
        <el-col :span='7'>
          <el-input clearable placeholder="请输入内容" v-model="dataForm.query" @clear='getData' >
            <el-button slot="append" icon="el-icon-search" @click='getData'></el-button>
          </el-input>
        </el-col>
        <el-col :span='4'>
          <el-button type="primary" @click='goadd'>添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="dataList" style="width: 100%" border stripe >
        <el-table-column type='index'></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width='110px'></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width='90px'></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width='150px'>
          <template slot-scope='scope'>
            {{ scope.row.add_time | timeInfo }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width='130px'>
          <template slot-scope='scope'>
            <el-button size='mini' type="primary" icon="el-icon-edit"></el-button>
            <el-button size='mini' type="danger" icon="el-icon-delete" @click='deleted(scope.row.goods_id)'></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[20, 30, 40, 50]"
        :page-size="20"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data(){
    return {
      dataForm:{
        query:'',
        pagenum:1,
        pagesize:20,
      },
      total:0,
      dataList:[],
    }
  },
  filters:{
    timeInfo(data){
      let time = new Date(data)
      return time.getFullYear() + '-' + time.getMonth() + 1 + '-' + time.getDate()
    }
  },
  methods:{
    async getData(){
      let res = await this.axios.get('/goods', { params:this.dataForm })
      if( res.data.meta.status == 200 ) {
        this.dataList = res.data.data.goods
        this.total = res.data.data.total
      } else {
        this.pop.error(res.data.meta.msg)
      }
    },
    handleSizeChange(size){
      this.dataForm.pagesize = size
      this.getData()
    },
    handleCurrentChange(num){
      this.dataForm.pagenum = num
      this.getData()
    },
    async deleted(id){
      let res = await this.confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch( err=>{
        return err
      })
      if( res != 'confirm' ) return this.pop('取消删除')
      res = await this.axios.delete(`/goods/${ id }`)
      if( res.data.meta.status == 200 ) {
        this.pop.success(res.data.meta.msg)
        this.getData()
      } else {
        this.pop.error(res.data.meta.msg)
      }
    },
    goadd(){
      this.$router.push('/goods/add')
    }
  },
  created(){
    this.getData()
  }
}
</script>

<style lang='scss' scoped>
.el-breadcrumb {
  padding:15px 0;
}
</style>